/**
 * @Description: 购物车
 * @Author: wrysunny
 * @Date: 2024-11-23 13:56:34
 * @LastEditor: wrysunny
 * @LastEditTime: 2024-11-23 15:43:55
 */
import {
	ref
} from "vue";
import {
	addCart,
	deleteCart,
	getCartList,
	updateCart
} from "@/config/api/goods";
import utils from "@/utils/utils";

const useCart = () => {
	const cartList = ref([]); // 购物车列表
	//获取购物车列表
	const getCartListFn = async () => {
		try {
			const res = await getCartList();
			if (res.code == 200) {
				cartList.value = res.data;
			}
		} catch (e) {
			//TODO handle the exception
		}
	}
	//添加购物车
	const addCartFn = async (data) => {
		try {
			const res = await addCart(data);
			if (res.code == 200) {
				utils.showToast('添加成功')
				// getCartListFn()
				setTimeout(() => {
					uni.navigateBack();
				}, 700)
			}
		} catch (e) {
			//TODO handle the exception
		}
	}

	//修改购物车
	const updateCartFn = async (data) => {
		try {
			const res = await updateCart(data);
			if (res.code == 200) {
				utils.showToast('修改成功')
				getCartListFn()
			}
		} catch (e) {
			//TODO handle the exception
		}
	}

	//删除购物车
	const deleteCartFn = async (data) => {
		try {
			const res = await deleteCart(data);
			if (res.code == 200) {
				utils.showToast('删除成功')
				getCartListFn()
			}
		} catch (e) {
			//TODO handle the exception
		}
	}
	return {
		cartList,
		addCartFn,
		updateCartFn,
		deleteCartFn,
		getCartListFn,
	}
};
export default useCart;